package cn.ilikes.tools.hbase.jpa.configs;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;

import cn.ilikes.tools.hbase.jpa.JPAHbasePersistenUnitEnv;
import cn.ilikes.tools.logs.Loggerfactory;

/**
 * 辅助类，主要用于加载 Entity Manager Factory 时，初始化相关环境。
 * 
 * 
 * 
 * @author fack
 * 
 */
public class Configure implements IConfigure {

	private static Logger logger = Loggerfactory.instance(Configure.class);

	/**
	 * createEntityManagerFactory(String pu, Map configs)
	 */
	private Properties configs;

	private List<IConfigure> configures = new ArrayList<IConfigure>();

	protected void addIConfigure(IConfigure configure) {
		configures.add(configure);
	}

	/**
	 * 
	 * @param configs
	 *                  : 是 JPAHbasePersistenceProvider 中
	 *                  createEntityManagerFactory的参数persistenceUnitsConfig
	 *                  EntityManagerFactory
	 *                  createEntityManagerFactory(String
	 *                  persistenceUnitsName, Map persistenceUnitsConfig)
	 */
	public Configure(Properties configs) {
		super();
		this.configs = configs;
		init();

	}

	/**
	 * config 初始化
	 */
	private void init() {

		//解析PersistenceUnit config 
		PersistenceUnitConfigure persistenceUnitConfigure = new PersistenceUnitConfigure(configs);
		addIConfigure(persistenceUnitConfigure);
		//解析PersistenceUnit 里的 IEntityMetaData

		//		PersistenceUnitClassloaderConfigure   persistenceUnitClassloaderConfigure =new PersistenceUnitClassloaderConfigure(configs);
		//		
		//		addIConfigure(persistenceUnitClassloaderConfigure);

		PersistenceIEntityMetaDataFactoryConfigure entityMetaDataFactoryConfigure = new PersistenceIEntityMetaDataFactoryConfigure(configs);
		addIConfigure(entityMetaDataFactoryConfigure);
	}

	/**
	 * configs config 。.init JPAHbasePersistenUnitEnv.
	 */
	@Override
	public void config(String persistenceUnitsName, JPAHbasePersistenUnitEnv hbasePersistenUnitEnv) throws JPAHBaseConfigurationException {
		Loggerfactory.info(logger, "JPA HBase Start Configure Persistence Units Name : " + persistenceUnitsName);
		for (IConfigure configure : configures) {
			configure.config(persistenceUnitsName, hbasePersistenUnitEnv);
		}
		hbasePersistenUnitEnv.setInitComplete(true);

	}

}
